//
//  RBLoginController.m
//  StoryToy
//
//  Created by baxiang on 2017/10/16.
//  Copyright © 2017年 roobo. All rights reserved.
//

#import "RBLoginController.h"
#import "RBRegisterController.h"
#import "UIImage+RBExtension.h"
#import "RTLineTextField.h"
#import "RTAppDelegate.h"
#import "RBForgetPsdController.h"

@interface RBLoginController ()<UITextFieldDelegate,UITextInputTraits>
@property (nonatomic, weak) RTLineTextField * accoutTxtV;///** 账号 */
@property (nonatomic, weak) RTLineTextField * psdTxtV;/** 密码 */
@property (nonatomic, weak) UIButton *loginBtn;/** 登录按钮 */
@end
@implementation RBLoginController

-(void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
     self.view.backgroundColor = [UIColor whiteColor];
    [[UIApplication sharedApplication] setStatusBarHidden:NO];
}
#pragma mark - 状态栏颜色
-(UIStatusBarStyle)preferredStatusBarStyle{
    return UIStatusBarStyleLightContent;
}

#pragma mark - viewDidLoad
- (void)viewDidLoad {
    [super viewDidLoad];
    [self.navigationController.navigationBar setHidden:NO];
    self.title = @"登录";
    RTLineTextField *accoutTxtV = [RTLineTextField new];
    [self.view addSubview:accoutTxtV];
    accoutTxtV.keyboardType = UIKeyboardTypePhonePad;
    [accoutTxtV setPlaceholder:@"请输入手机号"];
    accoutTxtV.textColor =UIColorHex(576166);
    accoutTxtV.font = [UIFont systemFontOfSize:13];
    accoutTxtV.returnKeyType = UIReturnKeyNext;
    [accoutTxtV mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(45);
        make.right.mas_equalTo(-45);
        make.top.mas_equalTo(89);
        make.height.mas_equalTo(45);
    }];
    _accoutTxtV = accoutTxtV;
    
    RTLineTextField *psdTxtV = [RTLineTextField new];
    [self.view addSubview:psdTxtV];
    [psdTxtV setPlaceholder:@"请输入6-20位密码"];
    psdTxtV.textColor = UIColorHex(0x576166);
    psdTxtV.font = [UIFont systemFontOfSize:13];
    psdTxtV.keyboardType = UIKeyboardTypeASCIICapable;
    [psdTxtV mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(45);
        make.right.mas_equalTo(-45);
        make.top.mas_equalTo(accoutTxtV.mas_bottom).offset(30);
        make.height.mas_equalTo(45);
    }];
    psdTxtV.secureTextEntry = YES;
    [psdTxtV.rac_textSignal subscribeNext:^(NSString * _Nullable x) {
        if ([x containsString:@" "]) {
            //[RBToastView showWithText:@"密码不能包含空格" inView:self.view];
        }
    }];
    _psdTxtV = psdTxtV;
    
    UIButton *secBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    [secBtn setImage:[UIImage imageNamed:@"icon_eyes_close"] forState:UIControlStateNormal];
    [secBtn setImage:[UIImage imageNamed:@"icon_eyes_open"] forState:UIControlStateSelected];
    secBtn.frame = CGRectMake(0, 0, 26, 13);
    psdTxtV.rightView = secBtn;
    psdTxtV.rightViewMode = UITextFieldViewModeAlways;
    @weakify(self);
    [[secBtn rac_signalForControlEvents:UIControlEventTouchUpInside] subscribeNext:^(__kindof UIControl * btn) {
        @strongify(self);
        [btn setSelected:!btn.isSelected];
        self.psdTxtV.secureTextEntry = !btn.isSelected;
    }];

    
    UIButton *registBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    [registBtn setTitle:@"快速注册" forState:UIControlStateNormal];
    registBtn.backgroundColor = [UIColor clearColor];
    registBtn.titleLabel.font = [UIFont systemFontOfSize:13];
    [registBtn setTitleColor:UIColorHex(0x626466) forState:UIControlStateNormal];
    [registBtn setImage:[UIImage imageNamed:@"icon_skip"] forState:UIControlStateNormal];
    [registBtn addTarget:self action:@selector(registAction:) forControlEvents:UIControlEventTouchUpInside];

    [self.view addSubview:registBtn];
    [registBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(psdTxtV.mas_left);
        make.height.mas_equalTo(40);
        make.bottom.mas_equalTo(psdTxtV.mas_bottom).offset(40);
    }];
    
    UIButton * forgetBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    [forgetBtn setTitle:@"忘记密码?" forState:UIControlStateNormal];
    forgetBtn.titleLabel.font = [UIFont systemFontOfSize:13];
    forgetBtn.titleLabel.textAlignment = NSTextAlignmentRight;
    forgetBtn.backgroundColor = [UIColor clearColor];
    [forgetBtn setTitleColor:UIColorHex(0x626466) forState:UIControlStateNormal];
    [forgetBtn addTarget:self action:@selector(foegetPsdAction:) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:forgetBtn];
    [forgetBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.mas_equalTo(accoutTxtV.mas_right);
        make.height.mas_equalTo(40);
        make.bottom.mas_equalTo(psdTxtV.mas_bottom).offset(40);
    }];
    
    UIButton*loginBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    [loginBtn setTitle:@"登录" forState:UIControlStateNormal];
    loginBtn.layer.cornerRadius = 45*0.5;
    loginBtn.layer.masksToBounds = YES;
    [loginBtn setBackgroundImage:[UIImage imageWithColor:RTColorForBackground] forState:UIControlStateNormal];
    [loginBtn setBackgroundImage:[UIImage imageWithColor:UIColorHex(0xc0c0c1)] forState:UIControlStateDisabled];
    [loginBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    [loginBtn addTarget:self action:@selector(loginAction) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:loginBtn];
    _loginBtn = loginBtn;
    [loginBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(accoutTxtV.mas_left);
        make.right.mas_equalTo(accoutTxtV.mas_right);
        make.height.mas_equalTo(accoutTxtV.mas_height);
        make.top.mas_equalTo(psdTxtV.mas_bottom).offset(100);
    }];
    
    [[self.accoutTxtV.rac_textSignal
      filter:^BOOL(NSString* text){
          return text.length == 11;
      }]
     subscribeNext:^(NSString* text){
          [self checkPhoneIsRegister:text];
     }];
    
    RAC(self.loginBtn,enabled) = [RACSignal combineLatest:@[accoutTxtV.rac_textSignal,psdTxtV.rac_textSignal] reduce:^id (NSString *phone,NSString*password){
        return @(phone.length>0 && password.length>5);
    }];
}

-(void)checkPhoneIsRegister:(NSString*)phoneCode
{
   
}

-(void)loginAction{
    NSString *phone = self.accoutTxtV.text;
    [RBUserApi login:phone passWord:self.psdTxtV.text pushToken:@"" block:^(RBUserModel * _Nonnull user, NSError * _Nonnull error) {
        NSArray *userDevice = user.devices;
        if(userDevice.count>0){
            RBDeviceModel *deviceModel = userDevice.firstObject;
            [RBAccessConfig saveDeviceID:deviceModel.deviceID];
        }
        [(RTAppDelegate*)[UIApplication sharedApplication].delegate switchRootViewController];
    }];
    // 第三方登录

//    [RBUserApi loginEx:phone passwd:self.psdTxtV.text thirdCode:@"xxxxxxxx" block:^(RBUserModel * _Nonnull user, NSError * _Nonnull error) {
//        if (error) {
//            NSLog(@"登录失败%@",error);
//            return;
//        }
//        NSArray *userDevice = user.devices;
//        if(userDevice.count>0){
//            RBDeviceModel *deviceModel = userDevice.firstObject;
//            [RBAccessConfig saveDeviceID:deviceModel.deviceID];
//        }
//        [(RTAppDelegate*)[UIApplication sharedApplication].delegate switchRootViewController];
//    }];
}

- (void)registAction:(UIButton*)btn{
    RBRegisterController *vc = [RBRegisterController new];
    [self.navigationController pushViewController:vc animated:YES];
}

- (void)foegetPsdAction:(UIButton*)btn{
    RBForgetPsdController*vc = [RBForgetPsdController new];;
    [self.navigationController pushViewController:vc animated:YES];
}

@end
